<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购单id" prop="purchaseId">
        <el-input
          v-model="queryParams.purchaseId"
          placeholder="请输入采购订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发运单号" prop="deliveryNo">
        <el-input
          v-model="queryParams.deliveryNo"
          placeholder="请输入发运单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.delivery_order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发运方式" prop="shipmentType">
        <el-select v-model="queryParams.shipmentType" placeholder="请选择发运方式" clearable>
          <el-option
            v-for="dict in dict.type.shipment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发运状态" prop="dispatchStatus">
        <el-select v-model="queryParams.dispatchStatus" placeholder="请选择发运单状态" clearable>
          <el-option
            v-for="dict in dict.type.part_dispatch_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发运时间" prop="dispatchTime">
        <el-date-picker clearable
          v-model="queryParams.dispatchTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发运时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="签收时间" prop="signForTime">
        <el-date-picker clearable
          v-model="queryParams.signForTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择签收时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="厂端id" prop="factoryId">
        <el-input
          v-model="queryParams.factoryId"
          placeholder="请输入厂端id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商id" prop="dealerId">
        <el-input
          v-model="queryParams.dealerId"
          placeholder="请输入经销商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商名" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人id" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人名" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['part:delivery:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['part:delivery:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['part:delivery:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['part:delivery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deliveryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="采购订单id" align="center" prop="purchaseId" />
      <el-table-column label="发运单号" align="center" prop="deliveryNo" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.delivery_order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="发运方式" align="center" prop="shipmentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shipment_type" :value="scope.row.shipmentType"/>
        </template>
      </el-table-column>
      <el-table-column label="发运单状态" align="center" prop="dispatchStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.part_dispatch_status" :value="scope.row.dispatchStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="发运时间" align="center" prop="dispatchTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dispatchTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签收时间" align="center" prop="signForTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signForTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="厂端id" align="center" prop="factoryId" />
      <el-table-column label="经销商id" align="center" prop="dealerId" />
      <el-table-column label="经销商名称" align="center" prop="dealerName" />
      <el-table-column label="发运单备注" align="center" prop="dispatchRemark" />
      <el-table-column label="审核人id" align="center" prop="auditorId" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.dispatchStatus==2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['part:delivery:edit']"
          >签收</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['part:delivery:remove']"-->
<!--            v-if="scope.row.dispatchStatus == 1"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改零件发运对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="采购订单id" prop="purchaseId">-->
<!--          <el-input v-model="form.purchaseId" placeholder="请输入采购订单id" />-->
<!--        </el-form-item>-->
        <el-form-item label="发运单号" prop="deliveryNo">
          <el-input v-model="form.deliveryNo" placeholder="请输入发运单号" :disabled="title!='添加零件发运'"/>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型" :disabled="title!='添加零件发运'">
            <el-option
              v-for="dict in dict.type.delivery_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发运方式" prop="shipmentType">
          <el-select v-model="form.shipmentType" placeholder="请选择发运方式" :disabled="title!='添加零件发运'">
            <el-option
              v-for="dict in dict.type.shipment_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发运单状态" prop="dispatchStatus">
          <el-select v-model="form.dispatchStatus" placeholder="请选择发运单状态">
            <el-option
              v-for="dict in dict.type.part_dispatch_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发运时间" prop="dispatchTime">
          <el-date-picker clearable
            v-model="form.dispatchTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发运时间"
            :disabled = "title!='添加零件发运'"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="签收时间" prop="signForTime" v-if="form.dispatchStatus==3">
          <el-date-picker clearable
            v-model="form.signForTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签收时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发运备注" prop="dispatchRemark">
          <el-input v-model="form.dispatchRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-divider content-position="center">零件采购单号</el-divider>

        <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange01">
          <el-table-column type="selection" width="55" align="center" v-if="title=='添加零件发运'"/>
          <el-table-column label="主键" align="center" prop="id" />
          <el-table-column label="厂端id" align="center" prop="factoryId" />
          <el-table-column label="经销商id" align="center" prop="dealerId" />
          <el-table-column label="经销商名称" align="center" prop="dealerName" />
          <el-table-column label="采购状态" align="center" prop="purchaseStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.part_purchase_status" :value="scope.row.purchaseStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="采购零件的数量" align="center" prop="wholePurchaseNumber" />
          <el-table-column label="采购的总价" align="center" prop="purchasePrice" />
          <el-table-column label="支付方式" align="center" prop="paymentMethod">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pay_type" :value="scope.row.paymentMethod"/>
            </template>
          </el-table-column>
          <el-table-column label="采购备注" align="center" prop="purchaseRemark" />
          <el-table-column label="仓库" align="center" prop="whseCode">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.warehouse_list" :value="scope.row.whseCode"/>
            </template>
          </el-table-column>
          <el-table-column label="期望交货时间" align="center" prop="expectedDeliveryDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.expectedDeliveryDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发运方式" align="center" prop="shipmentType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.shipment_type" :value="scope.row.shipmentType"/>
            </template>
          </el-table-column>
          <el-table-column label="收货人" align="center" prop="receiver" />
          <el-table-column label="联系电话" align="center" prop="telephone" />
          <el-table-column label="地址" align="center" prop="address" />
          <el-table-column label="审核人id" align="center" prop="auditorId" />
          <el-table-column label="审核人名称" align="center" prop="auditorName" />
          <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDelivery, getDelivery, delDelivery, addDelivery, updateDelivery,listPurchase } from "@/api/part/receipt";
import {getPartsInfoByIds, getPurchase} from "@/api/part/purchase";

export default {
  name: "Delivery",
  dicts: ['delivery_order_type', 'shipment_type', 'part_dispatch_status'],
  data() {
    return {
      //选中的采购订单的id
      purchaseIds:[],
      //零件采购的数据
      purchaseList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 零件发运表格数据
      deliveryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        purchaseId: null,
        deliveryNo: null,
        orderType: null,
        shipmentType: null,
        dispatchStatus: null,
        dispatchTime: null,
        signForTime: null,
        factoryId: null,
        dealerId: null,
        dealerName: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        purchaseId: [
          { required: true, message: "采购订单id不能为空", trigger: "blur" }
        ],
        deliveryNo: [
          { required: true, message: "发运单号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        shipmentType: [
          { required: true, message: "发运方式不能为空", trigger: "change" }
        ],
        dispatchStatus: [
          { required: true, message: "发运单状态不能为空", trigger: "change" }
        ],
        dispatchTime: [
          { required: true, message: "发运时间不能为空", trigger: "blur" }
        ],
        signForTime: [
          { required: true, message: "签收时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询零件发运列表 */
    getList() {
      this.loading = true;
      listDelivery(this.queryParams).then(response => {
        this.deliveryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // 查询参数
      this.queryParams= {
        pageNum: 1,
        pageSize: 10,
        id: null,
        purchaseId: null,
        deliveryNo: null,
        orderType: null,
        shipmentType: null,
        dispatchStatus: null,
        dispatchTime: null,
        signForTime: null,
        factoryId: null,
        dealerId: null,
        dealerName: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
      },

      this.form = {
        id: null,
        purchaseId: null,
        deliveryNo: null,
        orderType: null,
        shipmentType: null,
        dispatchStatus: null,
        dispatchTime: null,
        signForTime: null,
        factoryId: null,
        dealerId: null,
        dealerName: null,
        dispatchRemark: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.purchaseList = [];
      this.purchaseIds = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChange01(selection) {
      this.purchaseIds = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加零件发运";
      // 查询采购订单表(查询只能是待发货的采购订单）
      this.queryParams.purchaseStatus = 5;
      this.getPartPurchaseList();
    },
    getPartPurchaseList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDelivery(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改零件发运";
        this.purchaseIds = response.data.purchaseId;
        this.queryParams.id = this.purchaseIds;
        this.getPartPurchaseList();
        this.queryParams.id = null;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDelivery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //选择的采购订单只能一个
            if(this.purchaseIds.length!=1){
              this.$modal.msgError("采购订单有且只能选择一个！");
              return;
            }
            this.form.purchaseId = this.purchaseIds[0];
            addDelivery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除零件发运编号为"' + ids + '"的数据项？').then(function() {
        return delDelivery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('part/delivery/export', {
        ...this.queryParams
      }, `delivery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
